 /***************************************************************************
 *   Copyright (C) 2010 by ZhangShine , All Rights Reserved                                  
 *   zhangshine0125@gmail.com                                              
 *                                                                         
 *   This program is free software; you can redistribute it and/or modify  
 *   it under the terms of the GNU General Public License as published by  
 *   the Free Software Foundation; either version 2 of the License, or     
 *   (at your option) any later version.                                   
 *                                                                         
 *   This program is distributed in the hope that it will be useful,       
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
 *   GNU General Public License for more details.                          
 *                                                                         
 *   You should have received a copy of the GNU General Public License     
 *   along with this program; if not, write to the                         
 *   Free Software Foundation, Inc.,                                       
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             
 ***************************************************************************/

#include "http.h"
#include <QMessageBox>
#include <QApplication>
#include "sinaservices.h"

Http::Http(QObject *parent) :
    QObject(parent)
{
    this->isFinished = false;
    this->network = new QNetworkAccessManager(this);
    this->netReply = NULL;
    this->m_hasNetworkError = false;
    this->m_networkErrorCode = QNetworkReply::NoError;
    QObject::connect( this->network, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpFinished(QNetworkReply*)));
    QObject::connect( network, SIGNAL(finished(QNetworkReply*)), this, SIGNAL(finished()));
}

Http::~Http()
{
    //qDebug("http deleting");
    this->netReply=NULL;
}

QString Http::get(QNetworkRequest &request)
{
    this->netReply = network->get(request);
    while(!isFinished)
    {
        QApplication::processEvents();
    }
    QByteArray utf8byte = this->netReply->readAll();
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    
    if(this->netReply->error()==QNetworkReply::NoError)
    {
        /* No error */
    }
    else
    {
        /* Deal errors */
        handlerNetworkError(this->netReply->error());
    }
    QString result =codec->toUnicode(utf8byte);
    netReply->deleteLater();// it is my responsibility to delete the reply
    return result;
}

QPixmap Http::getImageFromUrl(QNetworkRequest &request)
{
    this->netReply = network->get(request);
    while(!isFinished)
    {
        QApplication::processEvents();
    }
    if(this->netReply->error()!=QNetworkReply::NoError)
    {
        /* Deal errors */
        handlerNetworkError(this->netReply->error());
    }
    QByteArray imageByte = this->netReply->readAll();
    QPixmap image;
    image.loadFromData(imageByte);
    netReply->deleteLater();
    return image;
}

QByteArray Http::getImageByteArrayFromUrl(QNetworkRequest &request)
{
    this->netReply = network->get(request);
    QObject::connect(netReply, SIGNAL(downloadProgress(qint64,qint64)), this, SIGNAL(downloadProgress(qint64,qint64)));
    while(!isFinished)
        QApplication::processEvents();
    if(netReply==NULL)
        return NULL;
    if(netReply->error()!=QNetworkReply::NoError)
        handlerNetworkError(netReply->error());
    QByteArray tmp = netReply->readAll();
    this->netReply->deleteLater();
    return tmp;
}

QString Http::post(QNetworkRequest &request, const QByteArray urlencodedAndUtf8FormatData)
{
    netReply = network->post(request, urlencodedAndUtf8FormatData);
    while(!isFinished)
        QApplication::processEvents();
    QByteArray utf8byte = netReply->readAll();
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    if(netReply->error()!=QNetworkReply::NoError)
        handlerNetworkError(netReply->error());
    QString result = codec->toUnicode(utf8byte);
    netReply->deleteLater();
    return result;
}

void Http::httpFinished(QNetworkReply *reply)
{
    this->netReply = reply;
    this->isFinished = true;
}

void Http::handlerNetworkError(QNetworkReply::NetworkError error)
{
    this->m_hasNetworkError = true;
    this->m_networkErrorCode = error;
}
